package com.evola.rm.view.project.tree
{
	import com.evola.rm.utils.FilterUtils;
	import com.evola.rm.view.project.ProjectModel;

	import mx.collections.ArrayCollection;
	import mx.collections.ICollectionView;
	import mx.controls.treeClasses.DefaultDataDescriptor;

	public class ResourceKeyTreeItemDescriptor extends DefaultDataDescriptor
	{

		private var _model:ProjectModel;

		public function ResourceKeyTreeItemDescriptor(model:ProjectModel)
		{
			super();

			_model=model;
		}

		override public function getChildren(node:Object, model:Object=null):ICollectionView
		{
			var children:ArrayCollection=new ArrayCollection([]);

			var folderItem:ResourceKeyTreeItem=node as ResourceKeyTreeItem;

			if (!_model.searchText)
			{
				// no filter being used, just return the children for Folder nodes


				return folderItem.children;
			}
			else
			{
				// filter the Folder's children

				for (var i:uint=0; i < folderItem.children.length; i++)
				{
					var child:ResourceKeyTreeItem=folderItem.children.getItemAt(i) as ResourceKeyTreeItem;
					if (FilterUtils.filterFunction(child, _model.searchText))
						children.addItem(child);
				}
			}

			return children;
		}

		override public function isBranch(node:Object, model:Object=null):Boolean
		{

			return hasChildren(node, model);
		}

	}
}
